﻿@model Tuple<int?>
@{
    ViewData["Title"] = "设备运维";
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">运维时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="startTime" class="layui-input date" id="test-laydate-start" placeholder="开始日期" lay-key="122">
                    </div>
                    <div class="layui-form-mid">
                        -
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" class="layui-input date" id="test-laydate-end" placeholder="结束时间" lay-key="123">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                    </button>
                    @if (1 != Model.Item1) { 
                        <button onclick="add()" class="layui-btn">添加运维计划</button>
                    }
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>


<script type="text/javascript" src="~/js/deviceState.js"></script>
<script>


    var laydate = layui.laydate;
    var insStart = laydate.render({
        elem: '#test-laydate-start'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#test-laydate-end'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });


        //加载table
    var tableObj= table.render({
            elem: '#LAY-app-content-list'
            , url: '/devicePlan/list/' //模拟接口
            ,method: 'post'
            , height:"full-110"
            , page: true
            , cols: [[
                { field: 'name', title: '计划名称', minWidth: 180, templet: '<div><a class="link" onclick="showDetail(\'{{d.id}}\')">{{d.name}}</a></div>' }
                , { field: 'name', title: '负责人', width: 100, templet: '<div>{{d.user}}</div>' }
                , { field: 'name', title: '参与人', width: 180, templet: '<div>{{d.userParticipate}}</div>' }
                , { field: 'name', title: '开始时间', width: 120, templet: '<div>{{ getDate(d.startTime)}}</div>' }
                , { field: 'name', title: '结束时间', width: 120, templet: '<div>{{ getDate(d.endTime)}}</div>' }
                , { field: 'name', title: '所属项目', width: 180, templet: '<div>{{d.project.name}}</div>' }
                , { field: 'name', title: '所属合同', width: 180, templet: '<div>{{getContract(d.contract)}}</div>' }
                , {
                    field: 'opr', title: '操作', width: 150, fixed:'right',
                templet:
                '<div class="layui-table-cell laytable-cell-11-0-8">'
                    + ' {{ getButtons(d.id) }} '
                + '</div>'
            }
            ]]
            , skin: 'line'
        });

    function getContract(d) {
        if (d != null) return d.title;
        return "";
    }

    function getDate(d) {
        if (d != null) {
            return d.substring(0, 10);
        } else {
            return "";
        }
    }

    //根据状态获取不同的按钮
    function getButtons(id) {
        var html = ' <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="edit(\'' + id +'\')" ><i class="layui-icon layui-icon-edit"></i>编辑</a>'
        html += ' <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\''+id+'\')"><i class="layui-icon layui-icon-delete"></i>删除</a> ';
        return html;
    }


    function deleteItem(id) {
        layer.confirm("确定要删除运维计划吗？删除后不可恢复！", { title: '删除' }, function (index) {
            layer.close(index);
            $.post("/deviceplan/Delete?id=" + id, function (d) {
                layer.msg(d.msg);
                if (d.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }

    function add() {
        window.winIndex = layer.open({
            type: 2,
            title: '添加运维计划',
            content: '/devicePlan/edit',
            area: ["1100px", "550px"]
        });
    }
    function edit(id) {
        window.winIndex = layer.open({
            type: 2,
            title: '添加运维计划',
            content: '/devicePlan/edit?id='+id,
            area: ["1100px", "550px"]
        });
    }

    function showDetail(id) {
        window.top.layer.open({
            type: 2,
            title: '查看运维计划',
            content: '/devicePlan/detail?id=' + id,
            area: ["1100px", "550px"]
        });
    }

    //添加设备到计划中
    var tableDevice = null;
    var tableSelectDevice = null;
    var planId = "";
    function addDeviceToPlan(deviceId) {
        $.ajax({
            url: "/devicePlan/AddDeviceToPlan",
            type: 'post',
            data: {
                deviceId: deviceId,
                planId: planId
            },
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {
                    tableDevice.reload();
                    tableSelectDevice.reload();
                }
            }
        });
    }
</script>